<?php

if (!defined('_CAN_LOAD_FILES_'))
	exit;

class Loyaltypts extends Module
{
	function __construct()
	{
		$this->name = 'loyaltypts';
		$this->tab = 'pricing_promotion';
		$this->version = '1.0';
		$this->author = 'CreativeCoder.pl';
		$this->need_instance = 1;

		parent::__construct();

		$this->displayName = $this->l('Program lojalnościowy soczewki.net');
		$this->description = $this->l('Zasada działania: 1pln=1pt bez uwzględniania kosztów dostawy');
		$this->confirmUninstall = $this->l('Are you sure you want to delete all loyalty points and customer history?');
	}


	function install()
	{
		if (!parent::install() OR !$this->installDB() 
                        OR !$this->registerHook('adminCustomers')
                        OR !Configuration::updateValue('PS_LOYALTYPST_POINT_VALUE', '1') 
                        OR !Configuration::updateValue('PS_LOYALTYPST_DELIVERED', '5') 
                        OR !Configuration::updateValue('PS_LOYALTYPTS_PLN_VALUE', '1'))
			return false;            
		return true;
	}

	function installDB()
	{

            Db::getInstance()->Execute('
		CREATE TABLE `'._DB_PREFIX_.'loyaltypts_level` (
			`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
			`points_from` INT UNSIGNED NOT NULL,
                        `points_to` INT UNSIGNED NOT NULL,
                        `discount` INT UNSIGNED NOT NULL,
			`created_at` DATETIME NOT NULL,
			`updated_at` DATETIME NOT NULL,
			PRIMARY KEY (`id`),
			INDEX index_loyaltypts_discount (`discount`)
		) DEFAULT CHARSET=utf8 ;');
            
            Db::getInstance()->Execute('
		CREATE TABLE `'._DB_PREFIX_.'loyaltypts` (
                        `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
			`id_customer` INT UNSIGNED NOT NULL,
                        `points` INT NOT NULL DEFAULT 0,
			PRIMARY KEY (`id`),
                        INDEX index_loyaltypts_id_customer (`id_customer`)
		) DEFAULT CHARSET=utf8 ;');       
            
            Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'loyaltypts_level`(`points_from`, `points_to`, `discount`, `created_at`) 
                VALUES(100, 300, 1, now()), 
                (301, 600, 2, now()),
                (601, 900, 3, now()),
                (901, 1500, 4, now()),
                (1501, 3000, 5, now()),
                (3001, 5000, 6, now()),
                (5001, 7999, 7, now()),
                (8000, 10000, 8, now());');

		return true;
	}
	
	function uninstall()
	{
		if (!parent::uninstall() OR !$this->uninstallDB() 
                        OR !Configuration::deleteByName('PS_LOYALTYPST_POINT_VALUE')	
                        OR !Configuration::deleteByName('PS_LOYALTYPTS_PLN_VALUE')
                        OR !Configuration::deleteByName('PS_LOYALTYPTS_DELIVERED'))
			return false;
		return true;
	}

	function uninstallDB()
	{
		Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'loyaltypts`;');
                Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'loyaltypts_level`;');
		return true;
	}
        
	/* Hook display in tab AdminCustomers on BO */
	public function hookAdminCustomers($params)
	{
		include_once(dirname(__FILE__).'/LoyaltyptsLevelModule.php');
		include_once(dirname(__FILE__).'/LoyaltyptsModule.php');
		
		$customer = new Customer((int)$params['id_customer']);
		if ($customer AND !Validate::isLoadedObject($customer))
			die(Tools::displayError('Incorrect object Customer.'));

		$points = (int)LoyaltyptsModule::getPointsByCustomer((int)$params['id_customer']);

		$html = '
		<br /><h2>'.$this->l('Loyalty points').' ('.(int)$points.' '.$this->l('points').')</h2>';
		
		if (!$points)
			return $html.' '.$this->l('This customer has no points');
		
//		$html .= '
//		<table cellspacing="0" cellpadding="0" class="table">
//			<tr style="background-color:#F5E9CF; padding: 0.3em 0.1em;">
//				<th>'.$this->l('Order').'</th>
//				<th>'.$this->l('Date').'</th>
//				<th>'.$this->l('Total (without shipping)').'</th>
//				<th>'.$this->l('Points').'</th>
//				<th>'.$this->l('Points Status').'</th>
//			</tr>';
//		foreach ($details AS $key => $loyalty)
//		{
//			$html.= '
//			<tr style="background-color: '.($key % 2 != 0 ? '#FFF6CF' : '#FFFFFF').';">
//				<td>'.((int)$loyalty['id'] > 0 ? '<a style="color: #268CCD; font-weight: bold; text-decoration: underline;" href="index.php?tab=AdminOrders&id_order='.$loyalty['id'].'&vieworder&token='.Tools::getAdminToken('AdminOrders'.(int)(Tab::getIdFromClassName('AdminOrders')).(int)($params['cookie']->id_employee)).'">'.$this->l('#').sprintf('%06d', $loyalty['id']).'</a>' : '--').'</td>
//				<td>'.Tools::displayDate($loyalty['date'], (int)($params['cookie']->id_lang)).'</td>
//				<td>'.((int)$loyalty['id'] > 0 ? $loyalty['total_without_shipping'] : '--').'</td>
//				<td>'.(int)$loyalty['points'].'</td>
//				<td>'.$loyalty['state'].'</td>
//			</tr>';
//		}
//		$html.= '
//			<tr>
//				<td>&nbsp;</td>
//				<td colspan="2" class="bold" style="text-align: right;">'.$this->l('Total points available:').'</td>
//				<td>'.$points.'</td>
//				<td>'.$this->l('Voucher value:').' '.Tools::displayPrice(LoyaltyModule::getVoucherValue((int)$points, (int)Configuration::get('PS_CURRENCY_DEFAULT')), new Currency((int)Configuration::get('PS_CURRENCY_DEFAULT'))).'</td>
//			</tr>
//		</table>';

		return $html;
	}        

}
